<?php
namespace app\admin\controller;
use app\Admin\model;
use think\Db;
use think\Request;
use think\Session;
class User extends Base {

    /**
     *会员列表
     */
    public function userList(){
        $this->isLogin();
        $row = Db::table("users")->where("user_id > 0")->paginate(8);
        $name = "";
        $page = $row->render();
        $this->view->assign("row",$row);
        $this->view->assign("row",$row);
        $this->view->assign("name",$name);
        $this->assign('page', $page);
        return view();
    }

    /**
     *
     * 查找会员
     */
    public function userSelect(){
        $this->isLogin();
        $request = Request::instance()->get();
        $name = $request['name'];
        $row = Db::table("users")->where("user_name like '%$name%' ")->paginate(8,false, [
            'query' => request()->param()
        ]);
        $page = $row->render();
        $this->view->assign("name",$name);
        $this->view->assign("row",$row);
        $this->assign('page', $page);
        return view("/user/userlist");
    }
    

    /**
     *会员封停，解封
     */
    public function userSet(){
        $this->isLogin();
        $request = Request::instance()->get();
        $id = $request['id'];
        $code = $request['code'];
        $m = new model\Users();
            if($code == 1){
            $row = $m->stopUser($id);
                echo json_encode($row);
        }elseif ($code == 2){
            $row = $m->startUser($id);
                echo json_encode($row);
        }
    }


    /**
     *会员订单管理(普通 and 高级)
     */
    public function userOrder(){ 
        $m = new model\Users();
        $row = $m->getList("orders");
        $this->view->assign('row',$row);
        return view();
    }


    /**
    *前台购买VIP(暂时放这里)
     */
    public function levelVip(){ 
        $re = Request::instance()->get();
        $id = $re['id'];
        $this->view->assign('id',$id);
        return view();

    }

    public function levelVips(){
        $re = Request::instance()->post();
        $id = $re['id'];
        $yi = $re['yi'];
        $m = new model\Users();
        $row = $m->setVip($id,$yi);
        if($row ==1){
            $this->success("会员开通成功",url('admin/user/userList'),2);
        }elseif ($row == 2){
            $this->error("会员开通失败",url('admin/user/userList'),2);
        }
    }

    public function isViptIme($id)
    {
        $m = new model\Users();
        $row = $m->getFind("time", "uid = 1");
        $dada = time();
        $endtim = $row['endtime'];
        if ($endtim <= $dada) {
            $row = Db::table('users')->where('user_id',$id)->update(['user_grade' => 0]);
            $this->error("您的会员已到期，请续费",url('admin/user/userList'),2);
        }else{
            return true;
        }

    }

}